package com.dxl.lanju.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.model.easyui.TreeNode;
import com.dxl.common.utils.Utils;
import com.dxl.lanju.dao.ProductCateDao;
import com.dxl.service.common.BaseService;
import com.lanju.bean.ProdCateTreeNode;
import com.lanju.entity.ProductCate;

@Service
@Transactional
public class ProductCateService extends BaseService {
	private static final Logger LOG = Logger.getLogger(ProductCateService.class);
	@Autowired
	private ProductCateDao productCateDao;
	
	public RespData save(ProductCate productCate) {
		ProductCate srcProductCate;
		if (Utils.isNotBlank(productCate.getId())) {
			srcProductCate = this.get(ProductCate.class, productCate.getId());
			srcProductCate.setName(productCate.getName());
			srcProductCate.setDescription(productCate.getDescription());
			srcProductCate.setState(productCate.getState());
			srcProductCate.setPath(productCate.getPath());
			srcProductCate.setPid(productCate.getPid());
		} else {
			if (this.productCateDao.exist(productCate.getName())) {
				return RespData.instance(RespData.FAIL, "品类已经存在！");
			}
			srcProductCate = productCate;
		}
		if (Utils.isBlank(srcProductCate.getPid())) {
			srcProductCate.setPid(null);
		}
		super.save(srcProductCate);
		return RespData.instance(RespData.SUCCESS, srcProductCate.getId());
	}
	
	public RespData del(String[] ids) {
		if (Utils.isEmpty(ids)) {
			return RespData.instance(RespData.SUCCESS);
		} else {
			try {
				this.del(ProductCate.class, ids);
				return RespData.instance(RespData.SUCCESS);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				return RespData.instance(RespData.FAIL);
			}
		}
	}
	
	public RespData state(String[] ids, Integer state) {
		if (Utils.isEmpty(ids)) {
			return RespData.instance(RespData.SUCCESS);
		} else {
			try {
				super.state(ProductCate.class, ids, state);
				return RespData.instance(RespData.SUCCESS);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
				return RespData.instance(RespData.FAIL);
			}
		}
	}
	
	public Page<ProductCate> list(String keyword, Integer state, Integer page, Integer rows) {
		return productCateDao.list(keyword, state, page, rows);
	}
	
	public List<ProdCateTreeNode> listTree() {
		List<ProdCateTreeNode> tree = new ArrayList<ProdCateTreeNode>(); 
		List<ProductCate> topAccounts = this.productCateDao.listCat(null, 1);
		if (Utils.isEmpty(topAccounts)) {
			return tree;
		}
		ProdCateTreeNode node;
		ProductCate prodCat;
		int size = topAccounts.size();
		for (int i = 0; i < size; i++) {
			prodCat = topAccounts.get(i);
			node = new ProdCateTreeNode(prodCat.getId(), prodCat.getName(), TreeNode.OPEN);
			node.setCreatedTime(prodCat.getCreatedTime());
			node.setDescription(prodCat.getDescription());
			node.setName(prodCat.getName());
			node.setStatus(prodCat.getState());
			node.setPath(prodCat.getPath());
			node.setUpdatedTime(prodCat.getUpdatedTime());
			this.listChildren(node);
			tree.add(node);
		}
		return tree;
	}
	
	private void listChildren(TreeNode node) {
		if (Utils.isNull(node)) {
			return;
		}
		List<ProductCate> children = this.productCateDao.listCat(node.getId(), null);
		if (Utils.isEmpty(children)) {
			return;
		}
		if (Utils.isBlank(node.getState())) {
			node.setState(TreeNode.CLOSED);
		}
		int size = children.size();
		ProductCate child;
		ProdCateTreeNode childNode;
		List<TreeNode> childNodes = new ArrayList<TreeNode>(size);
		for (int i = 0; i < size; i++) {
			child = children.get(i);
			childNode = new ProdCateTreeNode(child.getId(), child.getName());
			childNode.setCreatedTime(child.getCreatedTime());
			childNode.setDescription(child.getDescription());
			childNode.setName(child.getName());
			childNode.setStatus(child.getState());
			childNode.setPath(child.getPath());
			childNode.setUpdatedTime(child.getUpdatedTime());
			childNodes.add(childNode);
			node.setChildren(childNodes);
			listChildren(childNode);
		}
	}
}
